<template>
  <div class="outbound">
    <div class="query">
      <el-form
        :model="form"
        label-position="right"
        class="class-form"
      >
        <el-row :gutter="20">
          <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4">
            <el-form-item label="Sku">
              <el-input
                v-model="form.sku"
                style="width: 100%"
                clearable
                placeholder="Sku"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-form-item :label="$t('query.开始结束时间')">
            <el-date-picker
              v-model="form.date"
              type="daterange"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              style="width: 200px"
              :start-placeholder="$t('query.开始时间')"
              :end-placeholder="$t('query.结束时间')"
              clearable
            >
            </el-date-picker>
          </el-form-item>
          <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4">
            <el-form-item>
              <el-button
                type="primary"
                @click="
                  page.start = 1;
                  getDataList();
                "
                >{{ $t("form.queryBtn") }}</el-button
              >
              <el-button type="primary" @click="modelShow = true">{{
                $t("form.addBtn")
              }}</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>

    <div class="table-container">
      <el-table border :data="tableData">
        <el-table-column prop="id" label="id" align="center" minWidth="200">
        </el-table-column>
        <el-table-column
          prop="sku"
          :label="$t('outbound.商品sku')"
          align="center"
          minWidth="200"
        >
        </el-table-column>
        <el-table-column
          prop="product"
          :label="$t('outbound.商品中文名')"
          align="center"
          minWidth="150"
        >
        </el-table-column>
        <el-table-column
          prop="total"
          :label="$t('outbound.商品总数')"
          align="center"
          minWidth="100"
        >
        </el-table-column>
        <el-table-column
          prop="QTY"
          :label="$t('outbound.商品数量箱')"
          align="center"
          minWidth="100"
        >
        </el-table-column>
        <el-table-column
          prop="BOX"
          :label="$t('outbound.商品箱数')"
          align="center"
          minWidth="100"
        >
        </el-table-column>
        <el-table-column
          prop="PaymentDate"
          :label="$t('outbound.付款日期')"
          align="center"
          minWidth="200"
        >
        </el-table-column>
        <el-table-column
          prop="OutDate"
          :label="$t('outbound.出库日期')"
          align="center"
          minWidth="200"
        >
        </el-table-column>
        <el-table-column
          prop="remark"
          :label="$t('outbound.备注')"
          align="center"
          minWidth="200"
        >
        </el-table-column>
        <el-table-column
          prop="person"
          :label="$t('outbound.操作人')"
          align="center"
          minWidth="100"
        >
        </el-table-column>
        <el-table-column
          prop="LastDate"
          :label="$t('outbound.最后操作时间')"
          align="center"
          minWidth="200"
        >
        </el-table-column>
        <el-table-column
          :label="$t('outbound.修改')"
          align="center"
          minWidth="100"
          fixed="right"
        >
          <template slot-scope="{ row }">
            <el-button type="text" size="small" @click="editRow(row)">{{
              $t("form.editBtn")
            }}</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.start"
        background
        :page-sizes="[10, 100, 200, 300, 400]"
        :page-size="page.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>

    <el-dialog
      width="600px"
      :title="editForm.id ? '编辑' : '新增'"
      top="50px"
      :close-on-click-modal="false"
      :visible.sync="modelShow"
    >
      <el-form ref="formRef" :model="editForm" label-position="top">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item :label="$t('outbound.商品sku')">
              <el-input
                v-model="editForm.sku"
                :placeholder="$t('outbound.商品sku')"
                autocomplete="off"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('outbound.商品中文名')">
              <el-input
                :placeholder="$t('outbound.商品中文名')"
                v-model="editForm.product"
                autocomplete="off"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('outbound.商品总数')">
              <el-input-number
                v-model="editForm.total"
                :min="1"
                :placeholder="$t('outbound.商品总数')"
                label="商品总数"
              ></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('outbound.商品数量箱')">
              <el-input-number
                v-model="editForm.QTY"
                :min="1"
                :placeholder="$t('outbound.商品数量箱')"
                label="商品数量/箱"
              ></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('outbound.商品箱数')">
              <el-input-number
                v-model="editForm.BOX"
                :min="1"
                :placeholder="$t('outbound.商品箱数')"
                label="商品箱数"
              ></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('outbound.付款日期')">
              <el-date-picker
                v-model="editForm.PaymentDate"
                style="width: 100%"
                type="date"
                format="yyyy-MM-dd"
                value-format="yyyy-MM-dd"
                :placeholder="$t('outbound.付款日期')"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('outbound.出库日期')">
              <el-date-picker
                v-model="editForm.OutDate"
                type="date"
                format="yyyy-MM-dd"
                value-format="yyyy-MM-dd"
                style="width: 100%"
                :placeholder="$t('outbound.出库日期')"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('outbound.备注')">
              <el-input
                :placeholder="$t('outbound.备注')"
                v-model="editForm.remark"
                autocomplete="off"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('outbound.操作人')">
              <el-input
                :placeholder="$t('outbound.操作人')"
                v-model="editForm.person"
                autocomplete="off"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="modelShow = false">{{
          $t("form.cancleBtn")
        }}</el-button>
        <el-button type="primary" :loading="loading" @click="submit"
          >{{
          $t("form.sureBtn")
        }}</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { cloneDeep } from "loadsh";
import { CnOutDataApi, CnOutSelectApi, CnOutInApi, CnOutUpApi } from "@/api";
export default {
  name: "outbound",
  data() {
    return {
      page: {
        start: 1,
        limit: 10,
      },
      total: 0,
      form: {
        sku: "",
        StartDate: "",
        EndDate: "",
        date: [],
      },
      editForm: {
        sku: "",
        product: "",
        QTY: "",
        person: "",
        PaymentDate: "",
        BOX: "",
        total: "",
        remark: "",
        OutDate: "",
      },
      queryKey: "",
      tableData: [],
      modelShow: false,
      loading: false,
    };
  },
  created() {
    this.getDataList();
  },
  watch: {
    modelShow(value) {
      if (!value) {
        delete this.editForm.iid;
        this.resetForm();
      }
    },
  },
  methods: {
    async getDataList() {
      let params = cloneDeep({
        ...this.page,
        start: this.page.start - 1,
      });
      const formv = Object.keys(this.form).map((key) => this.form[key]);
      if (formv.every((item) => !item)) {
        const { data, total } = await CnOutSelectApi(params);
        this.tableData = data;
        this.total = total;
      } else {
        if (this.form.date) {
          this.form.StartDate = this.form.date[0];
          this.form.EndDate = this.form.date[1];
        }
        Object.keys(this.form).map((key) => {
          // extends date_property
          if (this.form[key] && key != "date") {
            params[key] = this.form[key];
          }
        });
        const { data, total } = await CnOutSelectApi(params);
        this.tableData = data;
        this.total = total;
      }
    },
    handleSizeChange(e) {
      this.page.limit = e;
      this.getDataList();
    },
    handleCurrentChange(e) {
      this.page.start = e;
      this.getDataList();
    },
    editRow(row) {
      this.editForm = cloneDeep(row);
      this.modelShow = true;
    },
    async submit() {
      const result = await this.$refs["formRef"].validate();
      if (!result) {
        return;
      }
      this.loading = true;
      delete this.editForm.OutDate
      try {
        if (Object.prototype.hasOwnProperty.call(this.editForm, "id")) {
          await CnOutUpApi(this.editForm);
        } else {
          await CnOutInApi(this.editForm);
        }
        this.getDataList();
        this.$message.success(this.editForm.id ? "修改成功" : "新增成功");
        this.modelShow = false;
        this.loading = false;
      } catch (error) {
        this.loading = false;
      }
    },
    resetForm() {
      this.$refs["formRef"].resetFields();
      Object.keys(this.editForm).forEach((key) => {
        this.editForm[key] = "";
      });
    },
  },
};
</script>

<style lang="scss" scoped src="./outbound.scss"></style>
